<?php

namespace Datacore\Service;


use Datacore\dao\UserDao;
use Datacore\model\userModel;

class UserService
{
    
    public static function getUserlist($where)
    {
        $userDao = new UserDao();
        $listEntity = $userDao->findByWhere($where);
        if ($listEntity) {
            foreach ($listEntity as $key => $Entity) {
                $list[$key]['id'] = $Entity->getId();
                $list[$key]['mobile'] = $Entity->getMobile();
                $list[$key]['username'] = $Entity->getUsername();
                $list[$key]['nickname'] = $Entity->getNickname();
            }
        } else {
            $list = array();
        }
        
        return $list;
    }
    
    public static function addUser($parms)
    {
        $Model   = new userModel();
        $Model->setMobile($parms['mobile']);
        $Model->setEmail($parms['email']);
        $Model->setusername($parms['username']);
        $Model->setPassword($parms['password']);
        $Model->setNickname($parms['nickname']);
        $Model->setSex($parms['sex']);
        $Model->setVip_time($parms['vip_time']);
        $Model->setAvatar($parms['avatar']);
        $Model->setStatus(1);
        $Model->setCreate_time(date('Y-m-d H:i:s',time()));
        $userDao = new UserDao();
        $res = $userDao->save($Model);
        return $res;
    }
    
    public static function updateUser(array $parms)
    {
        $Model   = new userModel();
        $userDao = new UserDao();
        $UserVO = $userDao->getOneById("id",$parms['id']);
        $Model->setid($parms['id']);
        $Model->setMobile($parms['mobile']!=""?$parms['mobile']:$UserVO->getMobile());
        $Model->setEmail($parms['email']!=""?$parms['email']:$UserVO->getEmail());
        $Model->setUsername($parms['username']!=""?$parms['username']:$UserVO->getUsername());
        $Model->setPassword($parms['password']!=""?$parms['mobile']:$UserVO->getPassword());
        $Model->setNickname($parms['nickname']!=""?$parms['nickname']:$UserVO->getNickname());
        $Model->setSex($parms['sex']!=""?$parms['sex']!="":$UserVO->getSex());
        $Model->setVip_time($parms['vip_time']!=""?$parms['vip_time']:$UserVO->getVip_time());
        $Model->setAvatar($parms['avatar']!=""?$parms['avatar']:$UserVO->getAvatar());
        $Model->setUpdate_time(date('Y-m-d H:i:s',time()));
        $userDao = new UserDao();
        $res = $userDao->update($Model);
        return $res;
    }
}